package com.eshop.modules.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eshop.common.service.impl.*;
import com.eshop.modules.user.param.AddressParam;
import com.eshop.modules.user.service.mapper.*;
import com.eshop.modules.user.domain.*;
import com.eshop.modules.user.service.*;
import com.eshop.utils.StringUtils;
import org.springframework.stereotype.*;
import org.springframework.transaction.annotation.*;
import com.eshop.dozer.service.*;
import com.eshop.enums.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.eshop.modules.user.vo.*;
import java.util.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import com.baomidou.mybatisplus.core.metadata.*;
import org.slf4j.*;

@Service
@Transactional(rollbackFor = { Exception.class })
public class UserAddressServiceImpl extends BaseServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService
{
    private static final Logger log;
    private final UserAddressMapper userAddressMapper;
    private final IGenerator generator;

    public void setDefault(final Long uid, final Long addressId) {
        final UserAddress address = new UserAddress();
        address.setIsDefault(ShopCommonEnum.DEFAULT_0.getValue());

        LambdaQueryWrapper<UserAddress> wrapper = new LambdaQueryWrapper();
        wrapper.eq(UserAddress::getUid,uid);
        this.userAddressMapper.update(address, wrapper);

        final UserAddress userAddress = new UserAddress();
        userAddress.setIsDefault(ShopCommonEnum.DEFAULT_1.getValue());
        userAddress.setId(addressId);
        this.userAddressMapper.updateById(userAddress);
    }

    @Override
    public Long addAndEdit(final Long uid, final AddressParam param) {
        //将所有参数全部赋值传给UserAddress，除了wx_export，is_default，id
        final UserAddress userAddress = UserAddress.builder().city(param.getAddress()
                .getCity()).cityId(param.getAddress().getCityId()).district(param.getAddress()
                .getDistrict()).province(param.getAddress().getProvince())
                .detail(param.getDetail()).uid(uid).phone(param.getPhone())
                .postCode(param.getPost_code()).realName(param.getReal_name()).build();
        //判断是否设置为默认地址
        if ("true".equals(param.getIs_default())) {
            userAddress.setIsDefault(ShopCommonEnum.DEFAULT_1.getValue());
            final UserAddress address = new UserAddress();
            address.setIsDefault(ShopCommonEnum.DEFAULT_0.getValue());
            //直接update，将当前用户的默认地址给设置成false
            ((UserAddressMapper)this.baseMapper).update(address, Wrappers.<UserAddress>lambdaQuery().eq(UserAddress::getUid, uid));
        }
        else {
            userAddress.setIsDefault(ShopCommonEnum.DEFAULT_0.getValue());
        }
        //如果前端传来的没有id，就说明是新增地址
        if (StrUtil.isBlank((CharSequence)param.getId())) {
            //这里保存的同时还自动注入了id，便于后面返回
            this.save(userAddress);
        }
        //否则就是修改地址
        else {
            //这里还顺便把默认地址给设置上了
            userAddress.setId(Long.valueOf(param.getId()));
            this.updateById(userAddress);
        }
        return userAddress.getId();
    }


    public UserAddressQueryVo getDetail(final Long id) {
        return this.generator.convert(this.getById(id), UserAddressQueryVo.class);
    }

    public List<UserAddressQueryVo> getList(final Long uid, final int page, final int limit) {
        final Page<UserAddress> pageModel =new Page(page, limit);
        final IPage<UserAddress> pageList = (this.lambdaQuery().eq(UserAddress::getUid, uid)).page(pageModel);
        return this.generator.convert(pageList.getRecords(), UserAddressQueryVo.class);
    }
    
    public UserAddress getUserDefaultAddress(final Long uid) {
        final LambdaQueryWrapper<UserAddress> wrapper = new LambdaQueryWrapper();
        ((wrapper.eq(UserAddress::getIsDefault, 1)).eq(UserAddress::getUid, uid)).last("limit 1");
        return this.getOne(wrapper);
    }

    public UserAddressServiceImpl(final UserAddressMapper userAddressMapper, final IGenerator generator) {
        this.userAddressMapper = userAddressMapper;
        this.generator = generator;
    }

    static {
        log = LoggerFactory.getLogger((Class)UserAddressServiceImpl.class);
    }
}
